package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.Listener.SubjectExcelListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.execl.SubjectData;
import com.atguigu.eduservice.entity.execl.oneLevelSubject;
import com.atguigu.eduservice.entity.execl.twoLevelSubject;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-04-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //    获得处理
//    Service有处理数据库的方法.
    @Autowired
    private  EduSubjectMapper eduSubjectMapper;

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) throws IOException {
        try {
//        文件输入流
//调用方法进行读取.
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List getAllSubject() {
        LambdaQueryWrapper<EduSubject> onelambdaQueryWrapper = new LambdaQueryWrapper();
        onelambdaQueryWrapper.eq(EduSubject::getParentId, "0");
        List<EduSubject> oneEduSubjects = eduSubjectMapper.selectList(onelambdaQueryWrapper);

        LambdaQueryWrapper<EduSubject> twolambdaQueryWrapper = new LambdaQueryWrapper();
        twolambdaQueryWrapper.ne(EduSubject::getParentId, "0");
        List<EduSubject> twoEduSubjects = eduSubjectMapper.selectList(twolambdaQueryWrapper);

        List allEduSubjects = new ArrayList();

        for (EduSubject oneeduSubject : oneEduSubjects) {
            oneLevelSubject oneLevelSubject = new oneLevelSubject();
            BeanUtils.copyProperties(oneeduSubject, oneLevelSubject);
            String id = oneeduSubject.getId();

            for (EduSubject twoeduSubject : twoEduSubjects) {

                if (id.equals(twoeduSubject.getParentId())) {
                    twoLevelSubject twoLevelSubject = new twoLevelSubject();
                    BeanUtils.copyProperties(twoeduSubject, twoLevelSubject);
                    oneLevelSubject.getChildren().add(twoLevelSubject);
                }

            }

            allEduSubjects.add(oneLevelSubject);
        }
        return allEduSubjects;
    }
}
